package com.future.mayi.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;

import java.util.ArrayList;
import java.util.List;

import static android.view.Gravity.CENTER;

/**
 * Created by 周大虎 on 2016/12/2.
 */

public class GoodsTabLayout extends RelativeLayout implements View.OnClickListener {
    private Context context;
    private ViewPager mViewPager;

    private View indicatorView;
    private int indicatorWeight = 20;
    private int indicatorHeight = 2;
    private int textSize = 15;

    private int indicatorColor = R.color.main_color;
    private int textColor = R.color.text_color9;
    private int selectedTextColor = R.color.main_color;

    private int maxWidth;
    private int maxHeight;


    private int dexWidth;
    private int itemWidth;


    public interface OnItemClickListener {
        void onItem(int position, int lastPosition);

        void onDoubleClick(int position);
    }


    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public GoodsTabLayout(Context context) {
        super(context);
        init(context);
    }

    public GoodsTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GoodsTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        indicatorWeight = CommonUtils.dip2px(context, indicatorWeight);
        indicatorHeight = CommonUtils.dip2px(context, indicatorHeight);
        setGravity(Gravity.BOTTOM);
        indicatorView = new View(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(indicatorWeight, indicatorHeight);
        indicatorView.setLayoutParams(layoutParams);

        maxWidth = CommonUtils.dip2px(context, 257);
        maxWidth = maxWidth > CommonUtils.getScreenSize(context)[0] ? CommonUtils.getScreenSize(context)[0] : maxWidth;
        maxHeight = CommonUtils.dip2px(context, 40);


    }


    public void setIndicatorWeightHeight(int indicatorWeight, int indicatorHeight) {
        if (indicatorView == null)
            return;
        this.indicatorWeight = CommonUtils.dip2px(context, indicatorWeight);
        this.indicatorHeight = CommonUtils.dip2px(context, indicatorHeight);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(this.indicatorWeight, this.indicatorHeight);
        indicatorView.setLayoutParams(layoutParams);
    }

    private List<TextView> textViewList;
    private int num = 0;

    public void setupWithViewPager(ViewPager mViewPager) {
        this.mViewPager = mViewPager;
        indicatorView.setBackgroundColor(getResources().getColor(indicatorColor));
        removeAllViews();
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(maxWidth, LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        addView(linearLayout);

        int size = mViewPager.getAdapter().getCount();
        itemWidth = maxWidth / size;
        dexWidth = (itemWidth - indicatorWeight) / 2;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, maxHeight);
        ViewGroup.LayoutParams tvlayoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textViewList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            LinearLayout llTv = new LinearLayout(getContext());
            llTv.setLayoutParams(layoutParams);
            llTv.setOrientation(LinearLayout.HORIZONTAL);
            llTv.setGravity(CENTER);

            TextView textView = new TextView(getContext());
            textView.setLayoutParams(tvlayoutParams);
            if (i == 0)
                textView.setTextColor(getResources().getColor(selectedTextColor));
            else
                textView.setTextColor(getResources().getColor(textColor));
            textView.setText(mViewPager.getAdapter().getPageTitle(i));
            textView.setBackgroundColor(getResources().getColor(R.color.transparent));
            textView.setTextSize(textSize);

            textView.setGravity(CENTER);
            textViewList.add(textView);


            llTv.setOnClickListener(this);
            llTv.setBackgroundResource(R.drawable.home_tab_onclick);
            llTv.setTag(i);
            llTv.addView(textView);
            linearLayout.addView(llTv);
        }

        addView(indicatorView);
        LayoutParams params = (LayoutParams) indicatorView.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        indicatorView.setLayoutParams(params);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                scrollTos(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                setItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        setItem(fristsetItem);
        post(new Runnable() {
            @Override
            public void run() {
                indicatorView.setTranslationX(dexWidth);
            }
        });

    }


    @Override
    public void onClick(View v) {
        if (v.getTag() instanceof Integer) {
            int position = (int) v.getTag();
            setItem(position);
        }
    }

    private int selected = 0;


    public void setItem(int selected) {

        if (this.selected == selected)
            return;


        int lastSelected = this.selected;


        if (textViewList == null || textViewList.size() == 0) {
            this.selected = selected;
            return;
        }


        this.selected = selected;

        if ((selected + 1) > textViewList.size())
            return;

        for (int i = 0; i < textViewList.size(); i++) {
            if (i == selected) {
                textViewList.get(i).setTextColor(getResources().getColor(selectedTextColor));
            } else {
                textViewList.get(i).setTextColor(getResources().getColor(textColor));
            }
        }
        mViewPager.setCurrentItem(selected);
        if (onItemClickListener != null)
            onItemClickListener.onItem(selected, lastSelected);
    }


    private void scrollTos(final int position, final float positionOffset) {
        post(new Runnable() {
            @Override
            public void run() {
                final int w = (int) (itemWidth * position + itemWidth * positionOffset + dexWidth);
                indicatorView.setTranslationX(w);
            }
        });
    }


    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (maxWidth == 0 && visibility == View.VISIBLE && mViewPager != null && textViewList == null) {
            num = 0;
            post(new Runnable() {
                @Override
                public void run() {
                    maxWidth = getMeasuredWidth();
                    maxHeight = getMeasuredHeight();
                }
            });
            setupWithViewPager(mViewPager);
        }

    }


    public List<TextView> getTextViewList() {
        return textViewList;
    }


    private int fristsetItem = 0;

    public void setFristsetItem(int fristsetItem) {
        this.fristsetItem = fristsetItem;
    }


}
